package com.lsh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lsh.common.HealthException;
import com.lsh.dao.CheckGroupAndCheckItemDao;
import com.lsh.dao.CheckGroupDao;
import com.lsh.entity.PageResult;
import com.lsh.entity.QueryPageBean;
import com.lsh.pojo.CheckGroup;
import com.lsh.service.CheckGroupService;
import com.lsh.utils.QiNiuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private CheckGroupAndCheckItemDao checkGroupAndCheckItemDao;
    /**
     * 添加检查组
     * @param checkGroup
     * @param checkitemIds
     */
    @Override
    @Transactional
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        //先添加检查组
        checkGroupDao.add(checkGroup);
        //再添加检查组与检查项的关系
        if (checkitemIds.length > 0 && checkitemIds != null) {
            for (Integer checkitemId : checkitemIds) {
                checkGroupAndCheckItemDao.add(checkGroup.getId(),checkitemId);
            }
        }

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //开始分页
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //查询数据
        List<CheckGroup> checkGroupList = checkGroupDao.findPage(queryPageBean);
        //封装返回数据
        PageInfo<CheckGroup> checkGroupPageInfo = new PageInfo<>(checkGroupList);
        PageResult pageResult = new PageResult(checkGroupPageInfo.getTotal(), checkGroupPageInfo.getList());
        return pageResult;
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }

    @Override
    public List<Integer> findCheckitemIdsByCheckGroupId(Integer id) {
        return checkGroupAndCheckItemDao.findCheckitemIdsByCheckGroupId(id);
    }

    @Override
    @Transactional
    public void update(CheckGroup checkGroup, Integer[] checkitemIds) {
        //先更新检查组
        checkGroupDao.update(checkGroup);
        //删除检查组检查项旧关系
        checkGroupAndCheckItemDao.deleteByCheckGroupId(checkGroup.getId());
        //添加新关系
        if(checkitemIds.length > 0 && checkitemIds != null){
            for (Integer checkitemId : checkitemIds) {
                checkGroupAndCheckItemDao.add(checkGroup.getId(),checkitemId);
            }
        }
    }
    @Transactional
    @Override
    public void deleteById(Integer id) {
        //先查询套餐表有没有使用检查组
        int count = checkGroupDao.findSetmealCountByCheckGroupId(id);
        if (count > 0){
            throw new HealthException("此检查项被套餐使用，无法删除");
        }
        //如果没有被使用
        //删除检查组和检查项关系
        checkGroupAndCheckItemDao.deleteByCheckGroupId(id);
        //删除检查组
        checkGroupDao.delete(id);

    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }
}
